package UILayer;
import ModelLayer.StaffMemberCollection;
import ModelLayer.CurrentAccount;
import java.util.Scanner;


/**
 * Write a description of class LoginUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoginUI
{
    MainMenuUI main_menu;

    /**
     * Constructor for objects of class LoginUI
     */
    public LoginUI()
    {
    }
    
    public void start()
    {
        boolean exit = false;
        while (!(exit))
        {
            int choice = write_menu();
            if (choice == 1)
            {
                login();
                
                
            }
            else if (choice == 2)
            {
                exit = true;
                System.out.println("Goodbye!\nThank you for using our system.");
                wait_for_input();
            }
            else
            {
                System.out.println("You typed a wrong number, please try again.");
                wait_for_input();
            }
        }
        System.out.println("\f");
    }
    
    private int write_menu()
    {
        int choice = 0;
        boolean ok = false;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\fLOGIN");
        System.out.println("Welcome, before continuing you need to log into your account");
        System.out.println("Press (1) to log in");
        System.out.println("Press (2) to exit the program");
        
        while(!(ok))
        {
            try
            {
                choice = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("Please type in a correct number");
                keyboard.nextLine();
            }
        }
        return choice;
    }
    /********************************************************INPUT READERS*****************************************************/
    private String string_input()
    {
        Scanner keyboard = new Scanner(System.in);
        String output;
        while((output = keyboard.nextLine()).equals(""))
        {
            
        }
        return output;
    }
    
    private String input_login()
    {
        System.out.println("Please write your login or write (0) to return:");
        return string_input();
    }
    
    private String input_password()
    {
        System.out.println("Please write your password:");
        return string_input();
    }
    
    /********************************************************MENU FUNCTIONS*****************************************************/

    private void login()
    {
        String login;
        String password;
        boolean correct = false;
        while (!(correct))
        {
            login = input_login();
            if(login.equals("0"))
            {
                break;
            }
            password = input_password();
            if((CurrentAccount.change_account(StaffMemberCollection.find_account(login, password))))
            {
                
                System.out.println("Welcome in the system, " + StaffMemberCollection.find_account(login, password).get_name() + "!");
                correct = true;
                wait_for_input();
                go_to_menu();
            }
            else
            {
                System.out.println("Account not found. Invalid login or password.");
                wait_for_input();
            }
            
        }
            
    }
    
    private void go_to_menu()
    {
        main_menu = new MainMenuUI();
        main_menu.start_menu();
    }
    
    /********************************************************OTHER FUNCTIONS*****************************************************/
    
    private void wait_for_input()
    {
        System.out.println("\nPRESS ENTER TO CONTINUE\n");
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
}
